﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

namespace Step4.Unit9.Filters
{
    public class CustomerResourceFilterAttribute : Attribute, IResourceFilter
    {
        private readonly Dictionary<string, IActionResult> dict = new(); // 可以这样实例化

        public void OnResourceExecuting(ResourceExecutingContext context)
        {
            var path = context.HttpContext.Request.Path; // 获取请求地址
            if (dict.ContainsKey(path))
            { 
                context.Result = dict[path];  // 直接返回缓存中对应的IActionResult
            }
        }

        public void OnResourceExecuted(ResourceExecutedContext context)
        {
            var path = context.HttpContext.Request.Path;
            dict[path] = context!.Result;
        }
    }
}
